/*
 * Copyright (c) Thorben Linneweber and others
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

using System;
using Jitter2.LinearMath;

using SoftFloat;

namespace Jitter2.Collision.Shapes
{
	
	/// <summary>
	/// Represents a single triangle within a mesh. The triangle is not flat but extends
	/// along its negative normal direction. This extension gives the triangle thickness,
	/// which can be controlled by the <see cref="Thickness"/> parameter.
	/// </summary>
	public class FatTriangleShape : TriangleShape
	{
	    private sfloat thickness;
	
	    /// <summary>
	    /// Set or get the thickness of the triangle.
	    /// </summary>
	    /// <exception cref="ArgumentException">Thickness must be larger than 0.01 length units.</exception>
	    public sfloat Thickness
	    {
	        get => thickness;
	        set
	        {
	            sfloat minimumThickness = (sfloat)0.01f;
	
	            if (value < minimumThickness)
	            {
	                throw new ArgumentException($"{nameof(Thickness)} must not be smaller than {minimumThickness}");
	            }
	
	            thickness = value;
	        }
	    }
	
	    /// <summary>
	    /// Initializes a new instance of the TriangleShape class.
	    /// </summary>
	    /// <param name="mesh">The triangle mesh to which this triangle belongs.</param>
	    /// <param name="index">The index representing the position of the triangle within the mesh.</param>
	    public FatTriangleShape(TriangleMesh mesh, int index, sfloat thickness /*= (sfloat)0.2f*/) : base(mesh, index)
	    {
	        this.thickness = thickness;
	        UpdateWorldBoundingBox(sfloat.Zero);
	    }
	
	    public override void CalculateBoundingBox(in NQuaternion orientation, in NVector3 position, out JBBox box)
	    {
	        ref var triangle = ref Mesh.Indices[Index];
	        var a = Mesh.Vertices[triangle.IndexA];
	        var b = Mesh.Vertices[triangle.IndexB];
	        var c = Mesh.Vertices[triangle.IndexC];
	
	        NVector3.Transform(a, orientation, out a);
	        NVector3.Transform(b, orientation, out b);
	        NVector3.Transform(c, orientation, out c);
	        NVector3.Transform(triangle.Normal, orientation, out NVector3 delta);
	
	        delta *= Thickness;
	
	        box = JBBox.SmallBox;
	
	        box.AddPoint(a);
	        box.AddPoint(b);
	        box.AddPoint(c);
	
	        box.AddPoint(a - delta);
	        box.AddPoint(b - delta);
	        box.AddPoint(c - delta);
	
	        box.Min += position;
	        box.Max += position;
	    
	        // 考虑位置和中心偏移
	        box.Min += centerOffset;
	        box.Max += centerOffset;
		}
	
	    public override void SupportMap(in NVector3 direction, out NVector3 result)
	    {
	        ref var triangle = ref Mesh.Indices[Index];
	
	        NVector3 a = Mesh.Vertices[triangle.IndexA];
	        NVector3 b = Mesh.Vertices[triangle.IndexB];
	        NVector3 c = Mesh.Vertices[triangle.IndexC];
	
	        sfloat min = NVector3.Dot(a, direction);
	        sfloat dot = NVector3.Dot(b, direction);
	
	        result = a;
	
	        if (dot > min)
	        {
	            min = dot;
	            result = b;
	        }
	
	        dot = NVector3.Dot(c, direction);
	
	        if (dot > min)
	        {
	            result = c;
	        }
	
	        if (NVector3.Dot(triangle.Normal, direction) < (sfloat)0.0f)
	            result -= triangle.Normal * Thickness;
	    
	        result += centerOffset;// 应用中心偏移
		}
	}
}
